﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace QLK.Database.DALs
{
    public class TransferDAL
    {
        private ManageStoreDataContext db;

        public TransferDAL()
        {
            db = new ManageStoreDataContext();
        }

        public List<Transfer> TransferGetAll()
        {
            var Trans = from t in db.Transfers
                        select t;

            return Trans.ToList<Transfer>();
        }

        public Transfer TransferGetByID(int TransID)
        {
            Transfer Trans = db.Transfers.Single<Transfer>(t => t.TransferID == TransID);

            return Trans;
        }

        public bool TransferInsert(Transfer Trans)
        {
            try
            {
                db.Transfers.InsertOnSubmit(Trans);
                db.SubmitChanges();
                return true;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                return false;
            }
        }

        public bool TransferUpdate(Transfer Trans)
        {
            Transfer Tran = db.Transfers.Single<Transfer>(t => t.TransferID == Trans.TransferID);

            if (Tran == null)
                return false;

            Tran.DepartureID = Trans.DepartureID;
            Tran.DestinationID = Trans.DestinationID;
            Tran.SenderID = Trans.SenderID;
            Tran.TransporterID = Trans.TransporterID;
            Tran.ReceiverID = Trans.ReceiverID;
            
            try
            {
                db.SubmitChanges();
                return true;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                return false;
            }
        }

        public bool TransferDeleteByID(int TransID)
        {
            Transfer Trans = db.Transfers.Single<Transfer>(t => t.TransferID == TransID);

            if (Trans == null)
                return false;

            db.Transfers.DeleteOnSubmit(Trans);

            try
            {
                db.SubmitChanges();
                return true;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                return false;
            }
        }

    }
}
